//
//  ASHomeApi.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/27.
//

#import "ASHomeApi.h"
#import "ASCommentModel.h"
#import "ASHomeBannerModel.h"
#import "ASHomeModel.h"
#import "ASServiceModel.h"
#import "ASSearchHotModel.h"
#import "ASAvatarClassHomeModel.h"
#import "ASDoutuHeadModel.h"
#import "ASTextModel.h"

@implementation ASHomeApi

/**
 获取服务端时间
 */
+(void)updateNetTimeCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"opinion/public_timestamp";
    entity.targetClass = [ASBaseModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}
/**
 获取全局推荐位
 */
+(void)loadCommendCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"opinion/self_advertising";
    entity.targetClass = [ASCommentModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 获取首页头部数据
 */
+(void)loadHomeBannerCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"index/head";
    entity.targetClass = [ASHomeBannerModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 获取首页数据
 */
+(void)loadHomeHomeListPage:(NSInteger)page Completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"index/index";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%zd",page] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",PAGESIZE_IG] forKey:@"pagesize"];
    if (page == 1) {
        [dic setObject:@"1" forKey:@"is_featured"];
    }else{
        [dic setObject:@"0" forKey:@"is_featured"];
    }
    [dic setObject:@"1" forKey:@"is_random"];
    entity.params = dic;
    entity.targetClass = [ASHomeModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 加载热门搜索数据
 */
+(void)loadHotSearchDataCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"index/popularSearches";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"10"] forKey:@"pagesize"];
    entity.params = dic;
    entity.targetClass = [ASBaseModel class];
    entity.subArrClass = [ASSearchHotModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  搜索数据
 type:0、其它，1、头像，2、表情，
*/
+(void)loadSearchData:(NSString *)searchContent page:(NSInteger)page type:(NSString *)type lbfl:(NSString * _Nullable)lbfl completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"index/search";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%zd",page] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",PAGESIZE_IG] forKey:@"pagesize"];
    [dic setObject:[NSString stringWithFormat:@"%@",searchContent] forKey:@"word"];
    [dic setObject:[NSString stringWithFormat:@"%@",type] forKey:@"type"];
    if ([lbfl isNotBlank]) {
        [dic setObject:[NSString stringWithFormat:@"%@",lbfl] forKey:@"classify_id"];
    }

    entity.params = dic;
    entity.targetClass = [ASBaseModel class];
    entity.subArrClass = [ASHomeMainModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  斗图表情分类列表
*/
+(void)loadDoutuDataCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"facial_expression/classifyList";
    entity.targetClass = [ASBaseModel class];
    entity.subArrClass = [ASDoutuHeadModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/// 百度人像动漫
/// @param image 图片
/// @param completion 成功
/// @param failure 失败
+(void)baiduAnimeImageWithImage:(UIImage *)image loadingStr:(NSString *__nullable)loadingStr Completion:(void (^)(UIImage *  _Nonnull image))completion failure:(void (^)(NSString * _Nonnull errorStr))failure{
    NSString *baiduToken = [ASServiceModel sharedManger].access_anime_token;;
    if (baiduToken.length < 1) {
        if (failure) {
            failure(@"百度ID获取失败");
        }
        return;
    }

    NSString *testURL = [NSString stringWithFormat:@"https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime?access_token=%@",baiduToken];
    testURL = [testURL  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:testURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方法
    request.HTTPMethod = @"POST";
    //设置请求体
    NSData *imageData = UIImagePNGRepresentation(image);
    NSString *baseStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    NSString *urlEncode = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                             NULL,
                                                                             (__bridge CFStringRef)baseStr,
                                                                             NULL,
                                                                             (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                          CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
    if (loadingStr.length > 0) {
        [MBProgressHUD showMessage:loadingStr];
    }else{
        [MBProgressHUD showMessage:@"制作中..."];

    }
    NSString *str =[NSString stringWithFormat:@"image=%@",urlEncode];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = data;
    NSURLSession *session  = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask =  [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
           if (error) {
               NSLog(@"error is %@",error);
               dispatch_async(dispatch_get_main_queue(), ^{
                   [MBProgressHUD hideHUD];
                   if (failure) {
                       failure(@"提取失败");
                   }
                });
           }else{
               NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
               if ([dic.allKeys containsObject:@"image"]) {
                   NSData *decodeData = [[NSData alloc]initWithBase64EncodedString:[dic objectForKey:@"image"] options:(NSDataBase64DecodingIgnoreUnknownCharacters)];
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [MBProgressHUD hideHUD];
                       if (decodeData) {
                           UIImage *image = [UIImage imageWithData:decodeData];
                           if (completion) {
                               completion(image);
                           }
                       }else{
                           if (failure) {
                               failure(@"提取失败");
                           }
                       }
                   });
               }else if ([dic.allKeys containsObject:@"error_msg"]) {
                   NSString *errStr = [dic objectForKey:@"error_msg"];
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [MBProgressHUD hideHUD];
                       if (failure) {
                           failure(errStr);
                       }
                   });
               }else{
                   dispatch_async(dispatch_get_main_queue(), ^{
                       [MBProgressHUD hideHUD];
                       if (failure) {
                           failure(@"提取失败");
                       }
                   });
               }
           }
    }];
    [dataTask resume];
}

/**
  加载头像分类首页数据
*/
+(void)loadAvatarClassDataCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"index/classifyList";
    entity.targetClass = [ASBaseModel class];
    entity.subArrClass = [ASAvatarClassHomeModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  加载头像分类列表数据
*/
+(void)loadAvatarList:(NSString *)subID page:(NSInteger)page Completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"user_upload/index";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%zd",page] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",PAGESIZE_IG] forKey:@"pagesize"];
    [dic setObject:subID?:@"" forKey:@"classify_id"];
    [dic setObject:@"1" forKey:@"type"];

    entity.params = dic;
    entity.targetClass = [ASHomeModel class];
    entity.subArrClass = [ASHomeMainModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  加载斗图表情列表数据
*/
+(void)loadDoutuList:(NSString *_Nullable)subID page:(NSInteger)page isFeatured:(NSString *_Nullable)isFeatured Completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"facial_expression/index";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%zd",page] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",PAGESIZE_IG] forKey:@"pagesize"];
    if (subID.length > 1) {
        [dic setObject:subID forKey:@"classify_id"];
    }else{
        int index = subID.intValue;
        if (index == 1) {
            [dic setObject:@"0" forKey:@"is_featured"];
        }else{
            [dic setObject:@"1" forKey:@"is_featured"];
        }
    }

    entity.params = dic;
    entity.targetClass = [ASHomeModel class];
    entity.subArrClass = [ASHomeMainModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  加载文案-分类数据
*/
+(void)loadTextTagDataCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"personality/classifyList";
    entity.targetClass = [ASHomeModel class];
    entity.subArrClass = [ASTextModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  加载文案列表数据
*/
+(void)loadTextListData:(NSInteger)page classifyId:(NSString *)classifyId Completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"personality/index";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%zd",page] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",PAGESIZE_IG] forKey:@"pagesize"];
    [dic setObject:classifyId?:@"" forKey:@"classify_id"];

    entity.params = dic;
    entity.targetClass = [ASHomeModel class];
    entity.subArrClass = [ASTextModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  加载文案搜索列表数据
*/
+(void)loadTextSearchListData:(NSInteger)page wordStr:(NSString *)wordStr Completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"personality/search";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%zd",page] forKey:@"page"];
    [dic setObject:[NSString stringWithFormat:@"%d",PAGESIZE_IG] forKey:@"pagesize"];
    [dic setObject:wordStr?:@"" forKey:@"word"];

    entity.params = dic;
    entity.targetClass = [ASHomeModel class];
    entity.subArrClass = [ASTextModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
  加载套图详情数据
*/
+(void)loadOneGroupData:(NSString *)subID Completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"user_upload/show";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:subID?:@"" forKey:@"id"];

    entity.params = dic;
    entity.targetClass = [ASHomeMainModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 加载全局公共数据
*/
+(void)loadOpinionDataCompletion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"other/globalParam";
    entity.targetClass = [ASServiceModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 下载统计
*/
+(void)addDownLoadTongji:(NSString *)subID completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"user_upload/downloadStatistics";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%@",subID] forKey:@"id"];
    entity.params = dic;
    entity.targetClass = [ASBaseModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 收藏统计
*/
+(void)addCollectionTongji:(NSString *)subID is_cancel:(NSString *)is_cancel completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"user_upload/collectStatistics";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%@",subID] forKey:@"id"];
    [dic setObject:[NSString stringWithFormat:@"%@",is_cancel] forKey:@"is_cancel"];
    entity.params = dic;
    entity.targetClass = [ASBaseModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}

/**
 浏览统计
*/
+(void)addSeeTongji:(NSString *)subID completion:(void (^)(id  _Nonnull result))completion failure:(void (^)(NSError * _Nonnull error))failure{
    BSDHTTPEntity *entity = [[BSDHTTPEntity alloc]init];
    entity.method = BSDHTTPMethodPOST;
    entity.path = @"user_upload/pvStatistics";
    NSMutableDictionary *dic = [NSMutableDictionary new];
    [dic setObject:[NSString stringWithFormat:@"%@",subID] forKey:@"id"];
    entity.params = dic;
    entity.targetClass = [ASBaseModel class];
    [[ASAPIClient sharedClient] requireWithEntity:entity completion:^(id  _Nonnull result) {
        completion(result);
    } failure:^(NSError * _Nonnull error) {
        failure(error);
    }];
}
@end
